//
//  WZGProtocolListVC.m
//  ZCJZ
//
//  Created by sujeking on 2019/12/23.
//  Copyright © 2019 jeking. All rights reserved.
//

#import "WZGProtocolListVC.h"
#import "WZGProtocolListCell.h"
#import "WZGProtocolListViewModel.h"
#import <Masonry.h>
#import "SKBaseWebService.h"
@interface WZGProtocolListVC ()
@property (strong, nonatomic) UITableView *tableView;
@property (assign, nonatomic) NSInteger page;
@property (nonatomic, strong) NSMutableArray *datasource;
@end

@implementation WZGProtocolListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self netFetchProtocolList];
}

- (void)setupUI {
    self.navTitle = @"服务与协议";
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    self.page = 1;
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderRefreshWithCallBack:^{
        weakSelf.page = 1;
        [weakSelf.datasource removeAllObjects];
        [weakSelf netFetchProtocolList];
    }];
    
    [self.tableView addFooterRefreshWithCallBack:^{
        ++weakSelf.page;
        [weakSelf netFetchProtocolList];
    }];
    
    [self setTableView:self.tableView noDataWithTitle:@"暂无数据" des:nil andImageName:nil];
}

// MARK: - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    WZGProtocolListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WZGProtocolListCell"];
    if (cell == nil) {
        cell = [[WZGProtocolListCell alloc] initWithFlex:nil reuseIdentifier:@"WZGProtocolListCell"];
    }
    cell.dict = self.datasource[row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 110.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSDictionary *dict = self.datasource[row];
    [WZGProtocolListViewModel loadProtolcolContentVCWithDict:dict];
}

// MARK: - NET

- (void)netFetchProtocolList {
    [self.nodataView removeFromSuperview];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(self.page) forKey:@"page"];
    [parameters setValue:@(50) forKey:@"pageSize"];
     __weak typeof(self) weakSelf = self;
    [SKBaseWebService getRequest:@"/v1/protocol/get-protocol-list" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, id data) {
        NSArray *arr = (NSArray *)data;
        
        if (arr == 0 && weakSelf.page != 0) {
            [weakSelf.view makeToast:@"没有更多了" duration:2.0 position:CSToastPositionCenter];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [weakSelf.datasource addObjectsFromArray:arr];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView endHeaderRefreshing];
        [weakSelf.tableView endFooterRefreshing];
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        [weakSelf.view makeToast:msg duration:2.0 position:CSToastPositionCenter];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    }];
}

// MARK: - GET

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UIColorFromRGB(0xf6f7f8);
    }
    return _tableView;
}


- (NSMutableArray *)datasource {
    if (_datasource == nil) {
        _datasource = [NSMutableArray new];
    }
    return _datasource;
}

@end
